//
//  ViewController.swift
//  SwiftWebViewBlockMenu
//
//  Created by 郑东喜 on 2017/3/24.
//  Copyright © 2017年 郑东喜. All rights reserved.
//

import UIKit
import WebKit

let SH = UIScreen.main.bounds.height
let SW = UIScreen.main.bounds.width

class ViewController: UIViewController {

    ///网页模板
    lazy var webView: WKWebView = {
        var wkV : WKWebView = WKWebView.init()
        
        //配置webview
        var configuration = WKWebViewConfiguration()
        let userContentController = WKUserContentController()
        
        // 禁止选择CSS
        let css = "body{-webkit-user-select:none;-webkit-user-drag:none;}"
        
        // CSS选中样式取消
        let javascript = NSMutableString.init()
        
        javascript.append("var style = document.createElement('style');")
        javascript.append("style.type = 'text/css';")
        javascript.appendFormat("var cssContent = document.createTextNode('%@');", css)
        javascript.append("style.appendChild(cssContent);")
        javascript.append("document.body.appendChild(style);")
        
        
        // javascript注入
        let noneSelectScript = WKUserScript.init(source: javascript as String, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
        
        userContentController.addUserScript(noneSelectScript)
        
        configuration.userContentController = userContentController
        
        ///由于设置了edgesForExtendedLayout,防止了页面全部控件向上偏移，所以在子页面数大于2的时候，矫正
        if (self.navigationController?.viewControllers.count)! >= 2 {
            let rect = CGRect(x: 0, y: 0, width: SW, height: SH - 64)
            wkV = WKWebView.init(frame: rect, configuration: configuration)
            //            wkV = WKWebView.init(frame: rect)
        } else {
            let rect = CGRect(x: 0, y: 0, width: SW, height: SH - 112)
            
            wkV = WKWebView.init(frame: rect, configuration: configuration)
            //            wkV = WKWebView.init(frame: rect)
        }
        
        return wkV
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        self.webView.load(URLRequest.init(url: URL.init(string: "http://www.pinggowang.com/app/preorder_detail.aspx?id=79&sku=7283274208")!))
        view.addSubview(webView)
    }

}

